
#include <SDL/SDL.h>

#include "screen.h"
#include "image.h"
#include "memory.h"
#include "object.h"


eruta_struct(Screen) {
  Image _surface; 
};  

Screen screen_make(int wide, int high, int depth, int doublebuffer) {   
    Uint32 bpp;
	Screen result = NULL;
    Uint32 flags = SDL_FULLSCREEN |  SDL_HWSURFACE ;
    if(doublebuffer) { flags = flags | SDL_DOUBLEBUF; }
    printf("Checking mode %dx%d@%dbpp.\n", wide, high, depth);
    bpp =   SDL_VideoModeOK(640, 480, 16, flags);
    if(!bpp){
        printf("Mode not available.\n");
        return NULL;
    }
    result = eruta_make(Screen);
    printf("SDL Recommends %dx%d@%dbpp.\n", wide,high, bpp);
    result->_surface = SDL_SetVideoMode(640, 480, bpp, flags);
    return result;
}

int screen_wide(Screen self) {
    return self->_surface->w;
}

int screen_high(Screen self) {
    return self->_surface->h;
}

Screen screen_flip(Screen self) {
    SDL_Flip(self->_surface);
    return self;
}

Screen screen_blit_size(Screen self, Image surface, 
        int src_x, int src_y, int src_w, int src_h, int tar_x, int tar_y ) 
{
   image_blit_size(self->_surface, tar_x, tar_y, surface, src_x, src_y, src_w, src_h);
   return self;
}


Screen screen_blit(Screen self, int tar_x, int tar_y, Image surface ) 
{
    image_blit(self->_surface, tar_x, tar_y, surface);
    return self;
}

Image screen_make_image(Screen self, int wide, int high) {
    Image result;
    Uint32 flags = SDL_HWSURFACE | SDL_SRCALPHA;
    result = SDL_CreateRGBSurface(flags, wide, high, 
                                  self->_surface->format->BitsPerPixel,
                                  self->_surface->format->Rmask, 
                                  self->_surface->format->Gmask, 
                                  self->_surface->format->Bmask, 
                                  self->_surface->format->Amask);
    return result;

}
